package community.hospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.annotation.*;
import community.hospital.common.BaseRemove;
import community.hospital.common.LoginUserHolder;
import community.hospital.common.R;
import community.hospital.convert.PageConvert;
import community.hospital.convert.UserConvert;
import community.hospital.entity.User;
import community.hospital.entity.request.*;
import community.hospital.entity.response.PageResponse;
import community.hospital.entity.response.UserInfoModel;
import community.hospital.entity.response.LoginResultModel;
import community.hospital.entity.response.UserResponse;
import community.hospital.service.application.UserApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import java.util.List;

import static community.hospital.common.Constant.*;
import static community.hospital.common.PermissionConstant.*;
import static community.hospital.common.RedisConstant.LOGIN_USER;
import static community.hospital.common.RedisConstant.QUERY_USER_CACHE;

/**
 * @Author：ydj
 * @Date：2023/1/16 16:44
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Module("用户")
public class UserController {

    @Resource
    private UserApplicationService userApplicationService;

    @PostMapping("/register")
    @Log(methodName = LOG_INSERT,desc = "用户注册")
    //TODO 开发完后打开验证 @Valid
    public R<Boolean> addUser(@RequestBody  AddUserRequest request) {
        Boolean isOk = userApplicationService.addUser(request);
        return R.ok(isOk);
    }

    @PostMapping("/login")
    @Log(methodName = LOG_LOGIN,desc = "用户登录")
    public R<LoginResultModel> login(@RequestBody @Valid LoginRequest request) {
          return userApplicationService.login(request);
    }

    @GetMapping("/getUserById")
    @NeedLogin
    @NeedPermission(role = SUPER_ADMIN, permission = QUERY_USER)
    @CacheQuery(value = QUERY_USER_CACHE,key = "#userId")
    @Log(methodName = LOG_QUERY,desc = "根据id查询用户信息")
    public R<UserResponse> getUserInfo(Long userId) {
        UserResponse userResponse = userApplicationService.getUserInfo(userId);
        return R.ok(userResponse);
    }

    @NeedLogin
    @GetMapping("/getPermCode")
    @Log(methodName = LOG_QUERY,desc = "查询当前用户权限信息")
    public R<List<String>> getPermCode() {
        UserResponse userResponse = LoginUserHolder.getUser();
        List<String> menuKey = userResponse.getMenuKey();
        return R.ok(menuKey);
    }

    @NeedLogin
    @GetMapping("/getUserInfo")
    @Log(methodName = LOG_QUERY,desc = "查询当前用户信息")
    public R<UserInfoModel> getUserInfoModel() {
        UserInfoModel userInfoModel = userApplicationService.getUserInfoModel();
        return R.ok(userInfoModel);
    }
    @GetMapping("/logout")
    @Log(methodName = LOG_LOGOUT,desc = "用户退出")
    public R<Boolean> logout(HttpServletRequest request) {
        Boolean isOk = userApplicationService.logout(request);
        return R.ok(isOk);
    }

    @NeedLogin
    @NeedPermission(role = {SUPER_ADMIN},permission = {LIST_USER})
    @PostMapping("/listUser")
    @Log(methodName = LOG_QUERY,desc = "分页查询用户权限信息")
    public R<Page<UserResponse>> listUser(@RequestBody @Valid ListUserRequest request) {
            Page<UserResponse> responseList = userApplicationService.listUser(request);
//        PageResponse<UserResponse> pageResponse = PageConvert.INSTANCE.converToPageResp(responseList);
        return R.ok(responseList);
    }

    @NeedLogin
    @NeedPermission(role = {SUPER_ADMIN}, permission = {REMOVE_USER})
    @PostMapping("/removeUser")
    @CacheRemove(value = {QUERY_USER_CACHE, LOGIN_USER}, key = "#request.ids")
    @Log(methodName = LOG_REMOVE,desc = "删除用户信息")
    public R<Boolean> removeUser(@Valid @RequestBody BaseRemove request) {
        Boolean isOk = userApplicationService.removeUser(request);
        return R.ok(isOk);
    }
    @NeedLogin
    @NeedPermission(role = {SUPER_ADMIN}, permission = {UPDATE_USER})
    @PostMapping("/updateUser")
    @CacheUpdate(value = {QUERY_USER_CACHE, LOGIN_USER}, key = "#request.id")
    @Log(methodName = LOG_UPDATE,desc = "更新用户信息")
    public R<Boolean> updateUser(@Valid @RequestBody UpdateUserRequest request) {
        Boolean isOk = userApplicationService.updateUser(request);
        return R.ok(isOk);
    }

    @PostMapping("/updatePassword")
    @NeedLogin
    @Log(methodName = LOG_UPDATE,desc = "更新密码")
    @NeedPermission(role = {SUPER_ADMIN}, permission = {UPDATE_USER})
    public R<Boolean> updatePassword(@Valid @RequestBody UpdatePasswordRequest request) {
        Boolean isOk = userApplicationService.updatePassword(request);
        return R.ok(isOk);
    }
    @PostMapping("/isAccountExit")
    public R<Boolean> isAccountExit(@RequestBody User user) {
        Boolean isOk = userApplicationService.isAccountExit(user);
        return R.ok(isOk);
    }

    @PostMapping("/updateCurrentUserPassword")
    @NeedLogin
    @Log(methodName = LOG_UPDATE,desc = "更新密码")
    public R<Boolean> updateCurrentUserPassword(@RequestBody @Valid PasswordRequest request) {
        Boolean isOk = userApplicationService.updateCurrentUserPassword(request);
        return R.ok(isOk);
    }

}
